package com.donger.business.upms.controller;


import cn.hutool.core.io.IoUtil;
import com.donger.business.upms.service.SmsService;
import com.donger.business.upms.service.impl.ImagesService;
import com.donger.common.core.annotation.IgnoreAuth;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码
 *
 * @author 7326
 */
@RestController
@RequestMapping("/captcha")
@AllArgsConstructor
public class CaptchaController {

    private final ImagesService imagesService;

    private final SmsService smsService;


    @IgnoreAuth
    @GetMapping("images.jpg")
    public void captcha(HttpServletResponse response, String uuid) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-config");
        response.setContentType("image/jpeg");
        //获取图片验证码
        BufferedImage image = imagesService.getCaptcha(uuid);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IoUtil.close(out);
    }


    @IgnoreAuth
    @GetMapping("/{mobile}")
    public Result sendSmsCode(@PathVariable String mobile) {
        String s = smsService.sendSmsCode(mobile);
        return Res.ok().data(s);
    }
}
